package cn.jbolt.ec.log;

import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.model.BecMember;
import cn.jbolt.common.model.BecOpLog;
import cn.jbolt.common.model.User;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.Calendar;

public class OpLogService extends JBoltBaseService<BecOpLog> {

    public final static String TYPE_BALANCE = "余额修改";
    public final static String TYPE_RETAILER_BALANCE = "商家余额修改";
    public final static String TYPE_CREDIT = "分销商修改";
    public final static String TYPE_SPREADER = "积分修改";
    public final static String TYPE_COUPON = "优惠券修改";
    private BecOpLog dao = new BecOpLog().dao();

    @Override
    protected BecOpLog dao() {
        return dao;
    }

    public  Ret add_log(String type , String before , String after ,Integer userId , String tbname,
                        Integer primary_id){
        return  add_log(type,before,after,userId,tbname,null,primary_id);
    }


    public Ret add_log(String type , String before , String after ,Integer userId , String tbname,
                       String memo,Integer primary_id){
        Long op_time = Calendar.getInstance().getTimeInMillis();

        if (notOk(type)|| notOk(before) ||notOk(after) || notOk(userId) ||
                notOk(tbname)|| notOk(primary_id))
            return fail("系统错误");

        BecOpLog becOpLog = new BecOpLog();
        becOpLog.setOpTime(op_time);
        becOpLog.setType(type);
        becOpLog.setBefore(before);
        becOpLog.setAfter(after);
        becOpLog.setUserid(userId);
        becOpLog.setTbname(tbname);
        becOpLog.setMemo(memo);
        becOpLog.setPrimaryId(primary_id);
        System.out.println(becOpLog);
        boolean success=becOpLog.save();

//
//        Record record =  new  Record().set("op_time",op_time)
//                .set("type",type)
//                .set("before",before)
//                .set("after",after)
//                .set("userId",userId)
//                .set("tbname",tbname)
//                .set("memo",memo)
//                .set("primary_id",primary_id);
//        boolean success = Db.save("bec_op_log",record);

        if (success){
            return SUCCESS;
        }
        return FAIL;
    }


    /************************************************************************************************
     * 后台日志管理
     * **********************************************************************************************/
    public Page<BecOpLog> paginate_Log(int pageNumber, int pageSize, String keywords,
                                       Integer onTime){

        long t1=System.currentTimeMillis();
        Kv kv = Kv.by("table",table());
        if (keywords!=null&&!keywords.equals("")){
            User user1 = new User();
            User user =   user1.dao().findFirst("select * from jb_user where name like ?","%"+keywords+"%");
            BecMember becMember1 = new BecMember();
            BecMember becMember = becMember1.dao().findFirst("select * from bec_member where nickname like ?","%"+keywords+"%");

            if (user!=null&&becMember!=null){
                kv.setIfNotNull("user_id",user.getId());
                kv.setIfNotNull("primary_id",becMember.getId());
            }
            else if (becMember!=null&&user==null){
                kv.setIfNotNull("primary_id",becMember.getId());
            }
            else  if (becMember==null&&user!=null){
                kv.setIfNotNull("user_id",user.getId());
            }
            else {
                kv.setIfNotBlank("keywords",keywords);
            }
        }




        if (onTime!=null)
        {
            long ontime_long = (long)onTime;
            long t2=t1-(ontime_long*1000*3600*24);
            kv.setIfNotNull("onTime",t2);

        }


        return dao().paginate(pageNumber,pageSize, Db.getSqlPara("ec.mall.manage.log.select_op_log",kv));
    }



}
